
import 'package:bot_toast/bot_toast.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:get_storage/get_storage.dart';
import 'package:gun/page/login_page.dart';

import 'audio/audio_player_helper.dart';
import 'entity/current_task_entity.dart';
import 'entity/setting_entity.dart';
import 'http/api_url.dart';


void main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // // await LoggerManager().initLogger();
  eventFactory = EventBus();
  await GetStorage.init();
  final box = GetStorage();
  if(box.hasData('setting_entity')){
    settingEntity = SettingEntity.fromJson(box.read("setting_entity"));
    // if(settingEntity.apiAddr != null){
    //   apiUrl = settingEntity.apiAddr!;
    // }
    // if(settingEntity.sip2ApiAddr != null){
    //   sip2ApiUrl = settingEntity.sip2ApiAddr!;
    // }

    // sip2ApiUrl = 'http://${settingEntity.apiAddr!}:60060';
    apiUrl = 'http://${settingEntity.apiAddr!}:60060';
  }else{
    settingEntity = SettingEntity();
  }
  // // FlutterError.onError = (FlutterErrorDetails details) async {
  // //   LoggerManager().write("error", '${details.exception}\r\n${details.stack}');
  // // };
  currentTask = CurrentTaskEntity();
  player = AudioPlayerHelper()..init();
  runApp(const MyApp());

}

late AudioPlayerHelper player;

late EventBus eventFactory;
late SettingEntity settingEntity;
late CurrentTaskEntity currentTask;


class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    SystemChrome.setEnabledSystemUIMode(SystemUiMode.manual, overlays: []);
    return  ScreenUtilInit(
      designSize: const Size(400, 711),
      minTextAdapt: true,
      splitScreenMode: true,
      builder: (_ , child) {
        return  GetMaterialApp(
            debugShowCheckedModeBanner: false,
            builder: BotToastInit(),
            home: const Login()
        );
      },
    );
  }
}
